<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 求解连续数列（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>已知连续正整数数列{K}&#61;K1,K2,K3…Ki的各个数相加之和为S&#xff0c;i&#61;N (0&lt;S&lt;100000, 0&lt;N&lt;100000), 求此数列K。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入包含两个参数&#xff0c;1&#xff09;连续正整数数列和S&#xff0c;2&#xff09;数列里数的个数N。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>如果有解输出数列K&#xff0c;如果无解输出-1。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">525 6</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">85 86 87 88 89 90</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p><code>3 5</code></p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">-1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题解题思路&#xff1a;</p> 
<p>由于要求连续正整数数列的和&#xff0c;因此&#xff0c;S / N 的结果必然是数列的中间值&#xff0c;比如 525 / 6 &#61; 87.5&#xff0c;由于6是偶数个&#xff0c;因此87.5其实就是 87 和 88 的中间值。</p> 
<p>再比如 9 / 3 &#61; 3&#xff0c;而9是 2 3 4 数列的和&#xff0c;而3是奇数个&#xff0c;因此3就是2 3 4 数列的中间值。</p> 
<p></p> 
<p>得到中间值后&#xff0c;我们就可以根据连续正整数数列半径求出连续数列两个边界值&#xff0c;如果左边界~右边界的元素之和为S&#xff0c;否则求得边界就是符合要求的。</p> 
<p></p> 
<p>最后&#xff0c;返回两个边界之内的值即可。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const [sum, n] &#61; line.split(&#34; &#34;).map(Number);
  console.log(getResult(sum, n));
});

function getResult(sum, n) {
  const mid &#61; Math.floor(sum / n);
  let left, right;

  if (n % 2 &#61;&#61; 0) {
    const half &#61; n / 2;
    left &#61; mid - half &#43; 1;
    right &#61; mid &#43; half;
  } else {
    const half &#61; (n - 1) / 2;
    left &#61; mid - half;
    right &#61; mid &#43; half;
  }

  const arr &#61; [];
  let total &#61; 0;
  for (let i &#61; left; i &lt;&#61; right; i&#43;&#43;) {
    arr.push(i);
    total &#43;&#61; i;
  }

  if (total &#61;&#61; sum) return arr.join(&#34; &#34;);
  else return &#34;-1&#34;;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  // 输入获取
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int sum &#61; sc.nextInt();
    int n &#61; sc.nextInt();

    System.out.println(getResult(sum, n));
  }

  // 算法入口
  public static String getResult(int sum, int n) {
    int mid &#61; sum / n;
    int left, right, half;

    if (n % 2 &#61;&#61; 0) {
      half &#61; n / 2;
      left &#61; mid - half &#43; 1;
    } else {
      half &#61; (n - 1) / 2;
      left &#61; mid - half;
    }
    right &#61; mid &#43; half;

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);

    int total &#61; 0;
    for (int i &#61; left; i &lt;&#61; right; i&#43;&#43;) {
      sj.add(i &#43; &#34;&#34;);
      total &#43;&#61; i;
    }

    if (total &#61;&#61; sum) return sj.toString();
    else return &#34;-1&#34;;
  }
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
sumV, n &#61; map(int, input().split())


# 算法入口
def getResult():
    mid &#61; sumV // n

    if n % 2 &#61;&#61; 0:
        half &#61; n // 2
        left &#61; mid - half &#43; 1
        right &#61; mid &#43; half
    else:
        half &#61; (n - 1) // 2
        left &#61; mid - half
        right &#61; mid &#43; half

    arr &#61; [i for i in range(left, right &#43; 1)]
    total &#61; sum(arr)

    if total !&#61; sumV:
        return &#34;-1&#34;
    else:
        return &#34; &#34;.join(map(str, arr))


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main() {
    int sum, n;
    scanf(&#34;%d %d&#34;, &amp;sum, &amp;n);

    int mid &#61; sum / n;

    int left, right, half;

    if(n % 2 &#61;&#61; 0) {
        half &#61; n / 2;
        left &#61; mid - half &#43; 1;
    } else {
        half &#61; (n - 1) / 2;
        left &#61; mid - half;
    }

    right &#61; mid &#43; half;

    char res[100000];

    int total &#61; 0;
    for(int i &#61; left; i &lt;&#61; right; i&#43;&#43;) {
        char tmp[1000];
        sprintf(tmp, &#34;%d&#34;, i);
        strcat(res, tmp);

        if(i &lt; right) {
            strcat(res, &#34; &#34;);
        }

        total &#43;&#61; i;
    }

    if(total &#61;&#61; sum) {
        puts(res);
    } else {
        puts(&#34;-1&#34;);
    }

    return 0;
}

</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>